package cn.hutool.postfix.generator.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.postfix.generator.lang.BaseLang;
import cn.hutool.postfix.generator.lang.Lang;
import cn.hutool.setting.Setting;
import cn.hutool.setting.SettingUtil;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author liuxiongkang
 */
public final class Config {
    @SuppressWarnings("all")
    public static final Setting COMMON_CONFIG = SettingUtil.get("config");
    public static final String SOURCE_PATH = CharSequenceUtil.emptyToDefault(Config.COMMON_CONFIG.get("hutool.source.path"), System.getProperty("user.dir")).concat("\\");
    public static final String RULE_COMMENT_PREFIX = COMMON_CONFIG.getStr("rule.comment.prefix", "");
    public static final boolean STATIC_IMPORT = COMMON_CONFIG.getBool("rule.use.static.imports", true);
    public static final String DEPENDENCY_MVN_COMMAND = COMMON_CONFIG.get("dependency.maven.command");
    public static final Collection<String> EXT_CLASS_INCLUDES = CollUtil.unmodifiable(params("ext.class.includes"));
    public static final Collection<String> INCLUDES_COMPONENT = CollUtil.unmodifiable(params("component.name.includes"));
    public static final Collection<String> EXCLUDES_COMPONENT = CollUtil.unmodifiable(params("component.name.excludes"));
    public static final Collection<Lang> LANG = CollUtil.unmodifiable(ClassUtil.scanPackageBySuper(ClassUtil.getPackage(BaseLang.class), BaseLang.class)//
            .stream().filter(c -> ArrayUtil.contains(COMMON_CONFIG.getStrings("lang.enables"), c.getSimpleName().toLowerCase()))//
            .map(c -> Convert.convert(new TypeReference<Class<Lang>>() {}, c))//
            .map(ReflectUtil::newInstance).collect(Collectors.toList()));

    private Config() {
        // do nothing
    }

    private static Collection<String> params(String key) {
        return Arrays.asList(ArrayUtil.defaultIfEmpty(COMMON_CONFIG.getStrings(key), new String[]{}));
    }
}
